var startPoints = $("#startPoints"),
        minPoints = $("#minPoints"),
        maxPoints = $("#maxPoints"),
        commodityId = $("#commodityId"),
        error_message = $("#error_message"),
        alertPanel = $(".alert"),
        page_url = decodeURI(location.href),
        expiredTime = $("#expiredTime"),
        availableTime = $("#availableTime"),
        auction_length = $("#auction_length"),
        sort = $("#sort"),
        start = page_url.indexOf("?");
commodityDesc = $("#commodityDesc");

// 预填借款人用户名
if (start !== -1) {
    loginName.val(page_url.substring(start + 1));
}
$("#loanProduct option[value='BANK']").remove();
$("#loanProduct option[value='BUSINESS']").remove();
// 初始化还款方式下拉列表
$('.multiselect').multiselect();


function initDatePicker(data) {
    availableTime.datetimepicker({
        prevText: '<i class="fa fa-chevron-left"></i>',
        nextText: '<i class="fa fa-chevron-right"></i>',
        showButtonPanel: false
    });
    // 设置默认日期
    availableTime.datetimepicker('setDate', (data ? new Date(data.startTime) : new Date()));

    expiredTime.datetimepicker({
        prevText: '<i class="fa fa-chevron-left"></i>',
        nextText: '<i class="fa fa-chevron-right"></i>',
        showButtonPanel: false
    });
    // 设置默认日期
    expiredTime.datetimepicker('setDate', (data ? new Date(data.endTime) : new Date()));
}

// 初始化标的信息
var currLoan;
var loanId = $("input[name=id]").val();
if (loanId && loanId !== '') {
    $.get("pointsystem/itemsGet/" + loanId, function (data) {
        if (data) {
            var loan = data;
            commodityId.multiselect('select', loan.commodityId);
            commodityId.multiselect('refresh');
            startPoints.val(loan.startPoints);
            minPoints.val(loan.minPoints);
            maxPoints.val(loan.maxPoints);
            $('button.multiselect:first').prop("disabled", true);
            sort.val(loan.sort);
            commodityDesc.val(loan.commodityDesc);
            $("#auction_length").val(loan.time_long);
        }
    });
} else {
    $("#auction_length").val("0");
}

function showError(msg) {
    error_message.text(msg);
    alertPanel.show();
}
function hideError() {
    error_message.text('');
    alertPanel.hide();
}
/**
 * 验证借款金额
 * @returns {Boolean}
 */
function checkAmount() {
    var raw = startPoints.val(), value = parseInt(raw);
    if (raw === '')
        return false;
    if (isNaN(value) || value != raw || value < 0) {
        showError("起始积分不正确");
        startPoints.val('');
        return false;
    }
    return true;
}
startPoints.on("change", checkAmount);

function checkMinAmount() {
    var raw = minPoints.val(), value = parseInt(raw);
    if (raw === '')
        return false;
    if (isNaN(value) || value != raw || value <= 0) {
        showError("最小增加积分不正确");
        minPoints.val('');
        return false;
    }
    return true;
}
minPoints.on("change", checkMinAmount);

/*function checkMaxAmount() {
 var raw = maxPoints.val(), value = parseInt(raw);
 if (raw === '')
 return false;
 if (isNaN(value) || value != raw || value <= 0) {
 showError("最大增加积分不正确");
 maxPoints.val('');
 return false;
 }
 return true;
 }
 maxPoints.on("change", checkMaxAmount);*/

$("input[type=text]").focus(function () {
    hideError();
});

var saveLoanBtn = $("#saveLoanBtn");
/**
 * 提交表单
 * 
 * @param {type} param
 */
saveLoanBtn.click(function () {
    // 检验标的金额
    if (startPoints.val() === '') {
        showError("请输入标的金额");
        return;
    } else if (!checkAmount())
        return;


    // 检验最小投标金额
    if (minPoints.val() === '') {
        showError("请输入最小投标金额");
        return;
    } else if (!checkMinAmount())
        return;
    /** 检验最小投标金额
     if (maxPoints.val() === '') {
     showError("请输入最大投标金额");
     return;
     } else if (!checkMaxAmount())
     return;
     
     if (parseInt(minPoints.val()) > parseInt(maxPoints.val()))
     {
     //showError("[最小递增金额]不能大于[最大递增金额]");
     //return;
     }*/

    if (new Date($("#expiredTime").val()) <= new Date($("#availableTime").val()))
    {
        showError("[起拍时间]不能晚于[截止时间]");
        return;
    }

    if (auction_length.val() === '') {
        showError("拍卖时长不能为空");
        return;
    }
    if (isNaN(auction_length.val())) {
        showError("拍卖时长必须为数字");
        return;
    }
    if (sort.val() === '') {
        showError("拍卖顺序不能为空");
        return;
    }
    if (isNaN(sort.val())) {
        showError("拍卖顺序必须为数字");
        return;
    }
    if(sort.val() == '0'){
        showError("拍卖顺序必须为大于0的数字");
        return;
    }
    var auctionId = $("input[name=auctionId]").val();
    var id = $("input[name=id]").val();
    $.get("pointsystem/countBySort", {"sort": sort.val(), "auctionId": auctionId, "auctionItemId": id}, function (result) {
        if (result.success) {
            showError("拍卖顺序已经存在，请重新选择拍卖顺序!");
            return false;
        } else {
            // 保存标的信息
            var data = sys.formToJson($("#loanForm"));
            sys.btnSpin(saveLoanBtn);
            $.post("pointsystem/save", data, function (result) {
                if (result.success) {
                    sys.alert(result.message, function () {
                        location.href = "pointsystem/auctionlist";
                    });

                } else {
                    sys.alert(result.message);
                }
            }).fail(function () {
                alert("服务器错误");
            });
        }
    });

});